<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="###Java Reflect(反射)机制详解 反射机制是在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。 反射机制主要提供了以下功能：-在运行时判断任意一个对象所属的类；-在运行时构造任意一个类的对象；-在运行时判断任意一个类所具有的成员变量和方法；-在运">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Java-Reflect(反射)机制详解">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Java-Advanced/Java-Reflect(反射)机制详解/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="###Java Reflect(反射)机制详解 反射机制是在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。 反射机制主要提供了以下功能：-在运行时判断任意一个对象所属的类；-在运行时构造任意一个类的对象；-在运行时判断任意一个类所具有的成员变量和方法；-在运">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:45:07.020Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java-Reflect(反射)机制详解">
<meta name="twitter:description" content="###Java Reflect(反射)机制详解 反射机制是在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。 反射机制主要提供了以下功能：-在运行时判断任意一个对象所属的类；-在运行时构造任意一个类的对象；-在运行时判断任意一个类所具有的成员变量和方法；-在运">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Java-Advanced/Java-Reflect(反射)机制详解/"/>





  <title>Java-Reflect(反射)机制详解 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Java-Advanced/Java-Reflect(反射)机制详解/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java-Reflect(反射)机制详解</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>###Java Reflect(反射)机制详解</p>
<p><em>反射机制是在运行状态中，对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法和属性；这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。</em></p>
<p><strong>反射机制主要提供了以下功能：<br>-在运行时判断任意一个对象所属的类；<br>-在运行时构造任意一个类的对象；<br>-在运行时判断任意一个类所具有的成员变量和方法；<br>-在运行时调用任意一个对象的方法；<br>-生成动态代理。</strong><br><a id="more"></a></p>
<p><strong><em>反射机制的相关API</em></strong><br><strong>通过一个对象获得完整的包名和类名</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 获得完整的包名和类名</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect1 &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		Reflect1 reflect1 = new Reflect1();</span><br><span class="line">		System.out.println(&quot;包名: &quot; + reflect1.getClass().getPackage());</span><br><span class="line">		System.out.println(&quot;类名: &quot; + reflect1.getClass().getName());</span><br><span class="line">		System.out.println(&quot;简单名: &quot; + reflect1.getClass().getSimpleName());</span><br><span class="line">		System.out.println(&quot;规范名: &quot; + reflect1.getClass().getCanonicalName());</span><br><span class="line">		System.out.println(&quot;类型名: &quot; + reflect1.getClass().getTypeName());</span><br><span class="line">		/**</span><br><span class="line">		 * 包名: package com.mazaiting</span><br><span class="line">		 * 类名: com.mazaiting.Reflect1</span><br><span class="line">		 * 简单名: Reflect1</span><br><span class="line">		 * 规范名: com.mazaiting.Reflect1</span><br><span class="line">		 * 类型名: com.mazaiting.Reflect1</span><br><span class="line">		 */</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>实例化Class类对象</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 实例化Class类对象</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect2 &#123;</span><br><span class="line">	public static void main(String[] args) throws ClassNotFoundException &#123;</span><br><span class="line">		Class&lt;?&gt; class1 = null;</span><br><span class="line">		Class&lt;?&gt; class2 = null;</span><br><span class="line">		Class&lt;?&gt; class3 = null;</span><br><span class="line">		</span><br><span class="line">		class1 = Class.forName(&quot;com.mazaiting.Reflect2&quot;);</span><br><span class="line">		class2 = new Reflect2().getClass();</span><br><span class="line">		class3 = Reflect2.class;</span><br><span class="line">		</span><br><span class="line">		System.out.println(&quot;类名: &quot; + class1.getName());</span><br><span class="line">		System.out.println(&quot;类名: &quot; + class2.getName());</span><br><span class="line">		System.out.println(&quot;类名: &quot; + class3.getName());</span><br><span class="line">		/**</span><br><span class="line">		 * 类名: com.mazaiting.Reflect2</span><br><span class="line">		 * 类名: com.mazaiting.Reflect2</span><br><span class="line">		 * 类名: com.mazaiting.Reflect2</span><br><span class="line">		 */		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>获取一个对象的父类与实现的接口</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 获取一个对象的父类与实现的接口</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect3 implements Serializable&#123;</span><br><span class="line">	public static void main(String[] args) throws ClassNotFoundException &#123;</span><br><span class="line">		Class&lt;?&gt; clazz = Class.forName(&quot;com.mazaiting.Reflect3&quot;);</span><br><span class="line">		Class&lt;?&gt; superclass = clazz.getSuperclass();</span><br><span class="line">		System.out.println(&quot;clazz的父类: &quot; + superclass.getName());</span><br><span class="line">		</span><br><span class="line">		Class&lt;?&gt;[] interfaces = clazz.getInterfaces();</span><br><span class="line">		for (int i = 0; i &lt; interfaces.length; i++)&#123;</span><br><span class="line">			System.out.println(&quot;接口&quot; + i + &quot;: &quot; + interfaces[i].getName());</span><br><span class="line">		&#125;</span><br><span class="line">		/**</span><br><span class="line">		 * clazz的父类: java.lang.Object</span><br><span class="line">		 * 接口0: java.io.Serializable</span><br><span class="line">		 */		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>获取某个类中的全部构造函数</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 获取某个类中的全部构造函数</span><br><span class="line"> * 通过反射机制实例化一个类的对象</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect4 &#123;</span><br><span class="line">	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException&#123;</span><br><span class="line">		Class&lt;?&gt; clazz = Class.forName(&quot;com.mazaiting.User&quot;);</span><br><span class="line">		</span><br><span class="line">		Constructor&lt;?&gt;[] constructors = clazz.getConstructors();</span><br><span class="line">		System.out.println(&quot;构造方法共 &quot; + constructors.length + &quot;个&quot;);</span><br><span class="line">		for (int i=0;i&lt;constructors.length; i++)&#123;</span><br><span class="line">			Class&lt;?&gt;[] parameterTypes = constructors[i].getParameterTypes();</span><br><span class="line">			System.out.print(&quot;cons[&quot; + i + &quot;] (&quot;);</span><br><span class="line">			for (int j=0; j&lt;parameterTypes.length; j++)&#123;</span><br><span class="line">				if (j == parameterTypes.length - 1)</span><br><span class="line">                    System.out.print(parameterTypes[j].getName());</span><br><span class="line">                else</span><br><span class="line">                    System.out.print(parameterTypes[j].getName() + &quot;,&quot;);</span><br><span class="line">			&#125;</span><br><span class="line">			System.out.println(&quot;)&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		/**</span><br><span class="line">		 * 构造方法共 3个</span><br><span class="line">		 * cons[0] (java.lang.String,int)</span><br><span class="line">		 * cons[1] (java.lang.String)</span><br><span class="line">		 * cons[2] ()</span><br><span class="line">		 */</span><br><span class="line">		</span><br><span class="line">		User user0 = (User) constructors[0].newInstance(&quot;mazaiting&quot;, 23);</span><br><span class="line">		System.out.println(user0.toString());</span><br><span class="line">		</span><br><span class="line">		User user1 = (User) constructors[1].newInstance(&quot;mazaiting&quot;);</span><br><span class="line">		System.out.println(user1.toString());</span><br><span class="line">		</span><br><span class="line">		User user2 = (User) constructors[2].newInstance();</span><br><span class="line">		System.out.println(user2.toString());</span><br><span class="line">		</span><br><span class="line">		/**</span><br><span class="line">		 * User [name=mazaiting, age=23]</span><br><span class="line">		 * User [name=mazaiting, age=0]</span><br><span class="line">		 * User [name=null, age=0]</span><br><span class="line">		 */		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">public class User &#123;</span><br><span class="line">	private String name;</span><br><span class="line">	private int age;</span><br><span class="line">	</span><br><span class="line">	public User() &#123;</span><br><span class="line">		super();</span><br><span class="line">	&#125;</span><br><span class="line">	public User(String name) &#123;</span><br><span class="line">		super();</span><br><span class="line">		this.name = name;</span><br><span class="line">	&#125;</span><br><span class="line">	public User(String name, int age) &#123;</span><br><span class="line">		super();</span><br><span class="line">		this.name = name;</span><br><span class="line">		this.age = age;</span><br><span class="line">	&#125;</span><br><span class="line">	public String getName() &#123;</span><br><span class="line">		return name;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setName(String name) &#123;</span><br><span class="line">		this.name = name;</span><br><span class="line">	&#125;</span><br><span class="line">	public int getAge() &#123;</span><br><span class="line">		return age;</span><br><span class="line">	&#125;</span><br><span class="line">	public void setAge(int age) &#123;</span><br><span class="line">		this.age = age;</span><br><span class="line">	&#125;</span><br><span class="line">	@Override</span><br><span class="line">	public String toString() &#123;</span><br><span class="line">		return &quot;User [name=&quot; + name + &quot;, age=&quot; + age + &quot;]&quot;;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>获取某个类的全部属性</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 获取某个类的全部属性</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect5 implements Serializable&#123;</span><br><span class="line">	private static final long serialVersionUID = -2862585049955236662L;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException&#123;</span><br><span class="line">		Class&lt;?&gt; clazz = Class.forName(&quot;com.mazaiting.Reflect5&quot;);</span><br><span class="line">		System.out.println(&quot;===============本类属性===============&quot;);</span><br><span class="line">		// 取得本类的全部属性</span><br><span class="line">		Field[] fields = clazz.getDeclaredFields();</span><br><span class="line">		for(int i=0;i&lt;fields.length; i++)&#123;</span><br><span class="line">			// 权限修饰</span><br><span class="line">			int modifiers = fields[i].getModifiers();</span><br><span class="line">			String mo = Modifier.toString(modifiers);</span><br><span class="line">			// 属性类型</span><br><span class="line">			Class&lt;?&gt; type = fields[i].getType();</span><br><span class="line">			System.out.println(mo + &quot; &quot; + type.getName() + &quot; &quot; + fields[i].getName() + &quot;;&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		System.out.println(&quot;==========实现的接口或者父类的属性==========&quot;);</span><br><span class="line">		</span><br><span class="line">		// 取得实现的接口或者父类的属性</span><br><span class="line">		Field[] fields2 = clazz.getFields();</span><br><span class="line">		for(int i=0;i&lt;fields2.length;i++)&#123;</span><br><span class="line">			int modifiers = fields2[i].getModifiers();</span><br><span class="line">			String mo = Modifier.toString(modifiers);</span><br><span class="line">			</span><br><span class="line">			Class&lt;?&gt; type = fields2[i].getType();</span><br><span class="line">			System.out.println(mo + &quot; &quot; + type.getName() + &quot; &quot; + fields2[i].getName() + &quot;;&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		/**</span><br><span class="line">		 * ===============本类属性===============</span><br><span class="line">		 * private static final long serialVersionUID;</span><br><span class="line">		 * ==========实现的接口或者父类的属性==========</span><br><span class="line">		 * </span><br><span class="line">		 */</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>获取某个类的全部方法</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 获取某个类的全部方法</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect6 implements Serializable&#123;</span><br><span class="line">	private static final long serialVersionUID = -2862585049955236662L;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) throws ClassNotFoundException &#123;</span><br><span class="line">		Class&lt;?&gt; clazz = Class.forName(&quot;com.mazaiting.User&quot;);</span><br><span class="line">		// 获取与父类的全部方法，如果要获取当前类的方法所有方法</span><br><span class="line">		// 则使用clazz.getDeclaredMethods()方法</span><br><span class="line">		Method[] methods = clazz.getMethods();</span><br><span class="line">	</span><br><span class="line">		for(int i=0;i&lt;methods.length;i++)&#123;</span><br><span class="line">			// 返回类型</span><br><span class="line">			Class&lt;?&gt; returnType = methods[i].getReturnType();</span><br><span class="line">			int modifiers = methods[i].getModifiers();</span><br><span class="line">			// 权限修饰符</span><br><span class="line">			String modifier = Modifier.toString(modifiers);</span><br><span class="line">			Class&lt;?&gt;[] parameterTypes = methods[i].getParameterTypes();</span><br><span class="line">            Class&lt;?&gt;[] exceptionTypes = methods[i].getExceptionTypes();</span><br><span class="line">            System.out.println(&quot;函数名: &quot; + methods[i].getName());</span><br><span class="line">			System.out.println(&quot;权限修饰: &quot; + modifier);</span><br><span class="line">            System.out.println(&quot;返回值: &quot; + returnType.getName());</span><br><span class="line">            System.out.println(&quot;参数个数: &quot; + parameterTypes.length);</span><br><span class="line">            System.out.println(&quot;异常个数: &quot; + exceptionTypes.length);</span><br><span class="line">            System.out.println(&quot;---------------------------------------&quot;);</span><br><span class="line">		&#125;</span><br><span class="line">		/**</span><br><span class="line">		 * 函数名: toString</span><br><span class="line">		 * 权限修饰: public</span><br><span class="line">		 * 返回值: java.lang.String</span><br><span class="line">		 * 参数个数: 0</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: getName</span><br><span class="line">		 * 权限修饰: public</span><br><span class="line">		 * 返回值: java.lang.String</span><br><span class="line">		 * 参数个数: 0</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: setName</span><br><span class="line">		 * 权限修饰: public</span><br><span class="line">		 * 返回值: void</span><br><span class="line">		 * 参数个数: 1</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: getAge</span><br><span class="line">		 * 权限修饰: public</span><br><span class="line">		 * 返回值: int</span><br><span class="line">		 * 参数个数: 0</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: setAge</span><br><span class="line">		 * 权限修饰: public</span><br><span class="line">		 * 返回值: void</span><br><span class="line">		 * 参数个数: 1</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: wait</span><br><span class="line">		 * 权限修饰: public final</span><br><span class="line">		 * 返回值: void</span><br><span class="line">		 * 参数个数: 0</span><br><span class="line">		 * 异常个数: 1</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: wait</span><br><span class="line">		 * 权限修饰: public final</span><br><span class="line">		 * 返回值: void</span><br><span class="line">		 * 参数个数: 2</span><br><span class="line">		 * 异常个数: 1</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: wait</span><br><span class="line">		 * 权限修饰: public final native</span><br><span class="line">		 * 返回值: void</span><br><span class="line">		 * 参数个数: 1</span><br><span class="line">		 * 异常个数: 1</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: equals</span><br><span class="line">		 * 权限修饰: public</span><br><span class="line">		 * 返回值: boolean</span><br><span class="line">		 * 参数个数: 1</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: hashCode</span><br><span class="line">		 * 权限修饰: public native</span><br><span class="line">		 * 返回值: int</span><br><span class="line">		 * 参数个数: 0</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: getClass</span><br><span class="line">		 * 权限修饰: public final native</span><br><span class="line">		 * 返回值: java.lang.Class</span><br><span class="line">		 * 参数个数: 0</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: notify</span><br><span class="line">		 * 权限修饰: public final native</span><br><span class="line">		 * 返回值: void</span><br><span class="line">		 * 参数个数: 0</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 * 函数名: notifyAll</span><br><span class="line">		 * 权限修饰: public final native</span><br><span class="line">		 * 返回值: void</span><br><span class="line">		 * 参数个数: 0</span><br><span class="line">		 * 异常个数: 0</span><br><span class="line">		 * ---------------------------------------</span><br><span class="line">		 */</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>通过反射机制调用某个类的方法</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 反射机制调用某个类的方法</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect7 implements Serializable&#123;</span><br><span class="line">	private static final long serialVersionUID = -2862585049955236662L;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException &#123;</span><br><span class="line">		Class&lt;?&gt; clazz = Class.forName(&quot;com.mazaiting.Reflect7&quot;);</span><br><span class="line">		</span><br><span class="line">		// 获取当前类的reflect1方法</span><br><span class="line">		Method reflect1 = clazz.getDeclaredMethod(&quot;reflect1&quot;);</span><br><span class="line">		// 执行方法</span><br><span class="line">		reflect1.invoke(clazz.newInstance());</span><br><span class="line">		</span><br><span class="line">		Method reflect2 = clazz.getDeclaredMethod(&quot;reflect2&quot;, int.class, String.class);</span><br><span class="line">		reflect2.invoke(clazz.newInstance(), 20, &quot;mazaiting&quot;);	</span><br><span class="line">		</span><br><span class="line">		/**</span><br><span class="line">		 * Java 反射机制 - 调用某个类的方法1.</span><br><span class="line">		 * Java 反射机制 - 调用某个类的方法2.</span><br><span class="line">		 * age -&gt; 20. name -&gt; mazaiting</span><br><span class="line">		 */</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public void reflect1() &#123;</span><br><span class="line">        System.out.println(&quot;Java 反射机制 - 调用某个类的方法1.&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    public void reflect2(int age, String name) &#123;</span><br><span class="line">        System.out.println(&quot;Java 反射机制 - 调用某个类的方法2.&quot;);</span><br><span class="line">        System.out.println(&quot;age -&gt; &quot; + age + &quot;. name -&gt; &quot; + name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>通过反射机制操作某个类的属性</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 反射机制更改属性值</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect8 implements Serializable&#123;</span><br><span class="line">	private static final long serialVersionUID = -2862585049955236662L;</span><br><span class="line">	private String proprety = null;</span><br><span class="line">	public static void main(String[] args) throws Exception&#123;</span><br><span class="line">		// 获取类</span><br><span class="line">		Class&lt;?&gt; clazz = Class.forName(&quot;com.mazaiting.Reflect8&quot;);</span><br><span class="line">		// 得到当前类的实例</span><br><span class="line">		Reflect8 instance = (Reflect8) clazz.newInstance();</span><br><span class="line">		// 获取要更改的属性</span><br><span class="line">		Field proprety = clazz.getDeclaredField(&quot;proprety&quot;);</span><br><span class="line">		// 设置可访问</span><br><span class="line">		proprety.setAccessible(true);</span><br><span class="line">		// 设置属性</span><br><span class="line">		proprety.set(instance, &quot;属性&quot;);</span><br><span class="line">		System.out.println(proprety.get(instance));		</span><br><span class="line">		/**</span><br><span class="line">		 * 属性</span><br><span class="line">		 */</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>反射机制的动态代理</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 反射机制的动态代理</span><br><span class="line"> * 在java中有三种类类加载器。</span><br><span class="line"> * 1) Bootstrap ClassLoader 此加载器采用c++编写，一般开发中很少见。</span><br><span class="line"> * 2) Extension ClassLoader 用来进行扩展类的加载，一般对应的是jrelibext目录中的类</span><br><span class="line"> * 3) AppClassLoader 加载classpath指定的类，是最常用的加载器。同时也是java中默认的加载器。</span><br><span class="line"> * 如果想要完成动态代理，首先需要定义一个InvocationHandler接口的子类，已完成代理的具体操作。</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect9 &#123;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) throws Exception&#123;</span><br><span class="line">		MyInvocationHandler invocationHandler = new MyInvocationHandler();</span><br><span class="line">		Subject sub = (Subject) invocationHandler.bind(new RealSubject());</span><br><span class="line">		String info = sub.say(&quot;mazaiting&quot;, 23);</span><br><span class="line">		System.out.println(info);</span><br><span class="line">		/**</span><br><span class="line">		 * mazaiting, 23</span><br><span class="line">		 */</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class MyInvocationHandler implements InvocationHandler&#123;</span><br><span class="line">	private Object obj = null;</span><br><span class="line">	public Object bind(Object obj) &#123;</span><br><span class="line">		this.obj = obj;</span><br><span class="line">		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), </span><br><span class="line">				obj.getClass().getInterfaces(), this);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable &#123;</span><br><span class="line">		Object temp = method.invoke(obj, args);</span><br><span class="line">		return temp;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class RealSubject implements Subject&#123;</span><br><span class="line"></span><br><span class="line">	@Override</span><br><span class="line">	public String say(String name, int age) &#123;</span><br><span class="line">		return name + &quot;, &quot; + age;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface Subject&#123;</span><br><span class="line">	String say(String name, int age);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong><em>实例</em></strong><br><strong>在泛型为Integer的ArrayList中存放一个String类型的对象。</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 在泛型为Integer的ArrayList中存放一个String类型的对象。</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect10 &#123;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) throws Exception&#123;</span><br><span class="line">		</span><br><span class="line">		ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();</span><br><span class="line">		</span><br><span class="line">		Method method = list.getClass().getDeclaredMethod(&quot;add&quot;, Object.class);</span><br><span class="line">		</span><br><span class="line">		method.invoke(list, &quot;Java反射机制实例&quot;);</span><br><span class="line">		</span><br><span class="line">		System.out.println(list.get(0));</span><br><span class="line">		/**</span><br><span class="line">		 * Java反射机制实例</span><br><span class="line">		 */</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>通过反射取得并修改数组的信息</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 反射取得并修改数组的信息</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect11 &#123;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) throws Exception&#123;</span><br><span class="line">		int[] temp = &#123;1, 2, 3, 4, 5&#125;;</span><br><span class="line">		Class&lt;?&gt; componentType = temp.getClass().getComponentType();</span><br><span class="line">		System.out.println(&quot;数组类型: &quot; + componentType.getName());</span><br><span class="line">        System.out.println(&quot;数组长度: &quot; + Array.getLength(temp));</span><br><span class="line">        System.out.println(&quot;数组的第一个元素: &quot; + Array.get(temp, 0));</span><br><span class="line">        // 设置第一个元素数据为100</span><br><span class="line">        Array.set(temp, 0, 100);</span><br><span class="line">        System.out.println(&quot;修改之后数组第一个元素为： &quot; + Array.get(temp, 0));</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>通过反射机制修改数组的大小</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 反射机制修改数组的大小</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect12 &#123;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) throws Exception &#123;</span><br><span class="line">        int[] temp = &#123; 1, 2, 3, 4, 5, 6, 7, 8, 9 &#125;;</span><br><span class="line">        int[] newTemp = (int[]) arrayInc(temp, 15);</span><br><span class="line">        print(newTemp);</span><br><span class="line">        String[] atr = &#123; &quot;a&quot;, &quot;b&quot;, &quot;c&quot; &#125;;</span><br><span class="line">        String[] str1 = (String[]) arrayInc(atr, 8);</span><br><span class="line">        print(str1);</span><br><span class="line">        /**</span><br><span class="line">         * 数组长度为： 15</span><br><span class="line">		 * 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 </span><br><span class="line">		 * 数组长度为： 8</span><br><span class="line">		 * a b c null null null null null </span><br><span class="line">         */</span><br><span class="line">    &#125;</span><br><span class="line">    // 修改数组大小</span><br><span class="line">    public static Object arrayInc(Object obj, int len) &#123;</span><br><span class="line">        Class&lt;?&gt; arr = obj.getClass().getComponentType();</span><br><span class="line">        Object newArr = Array.newInstance(arr, len);</span><br><span class="line">        int co = Array.getLength(obj);</span><br><span class="line">        System.arraycopy(obj, 0, newArr, 0, co);</span><br><span class="line">        return newArr;</span><br><span class="line">    &#125;</span><br><span class="line">    // 打印</span><br><span class="line">    public static void print(Object obj) &#123;</span><br><span class="line">        Class&lt;?&gt; c = obj.getClass();</span><br><span class="line">        if (!c.isArray()) &#123;</span><br><span class="line">            return;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(&quot;数组长度为： &quot; + Array.getLength(obj));</span><br><span class="line">        for (int i = 0; i &lt; Array.getLength(obj); i++) &#123;</span><br><span class="line">            System.out.print(Array.get(obj, i) + &quot; &quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><strong>将反射机制应用于工厂模式</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 反射机制应用于工厂模式</span><br><span class="line"> * @author mazaiting</span><br><span class="line"> */</span><br><span class="line">public class Reflect13 &#123;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) throws Exception &#123;</span><br><span class="line">//		Fruit f = Factory.getInstance(&quot;com.mazaiting.Orange&quot;);</span><br><span class="line">		Fruit f = Factory.getInstance(&quot;com.mazaiting.Apple&quot;);</span><br><span class="line">        if (f != null) &#123;</span><br><span class="line">            f.eat();</span><br><span class="line">        &#125;</span><br><span class="line">        /**</span><br><span class="line">         * Apple</span><br><span class="line">         */</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">interface Fruit &#123;</span><br><span class="line">    public abstract void eat();</span><br><span class="line">&#125;</span><br><span class="line">class Apple implements Fruit &#123;</span><br><span class="line">    public void eat() &#123;</span><br><span class="line">        System.out.println(&quot;Apple&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">class Orange implements Fruit &#123;</span><br><span class="line">    public void eat() &#123;</span><br><span class="line">        System.out.println(&quot;Orange&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">class Factory &#123;</span><br><span class="line">    public static Fruit getInstance(String ClassName) &#123;</span><br><span class="line">        Fruit f = null;</span><br><span class="line">        try &#123;</span><br><span class="line">            f = (Fruit) Class.forName(ClassName).newInstance();</span><br><span class="line">        &#125; catch (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        return f;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><a href="http://git.oschina.net/zaitingma/codes/f2yi0d87ap5zslq9ohugk83" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Java/Java-获取本机内外网ip/" rel="next" title="Java-获取本机内外网ip">
                <i class="fa fa-chevron-left"></i> Java-获取本机内外网ip
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Java-Advanced/Java-泛型/" rel="prev" title="Java-泛型">
                Java-泛型 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
